<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :title="popupTitle"
            :async="true"
            width="800px"
            :clickModalClose="true"
            @confirm="handleSubmit"
            @close="handleClose"
        >
            <el-form
                ref="formRef"
                :model="formData"
                label-width="100px"
                :rules="formRules"
                :inline="true"
            >
                <el-form-item label="案件类型" prop="typevalues">
                    <el-checkbox-group v-model="formData.typevalues" placeholder="请选择案件类型">
                        <el-checkbox
                            label="全选"
                            name="typevalues"
                            @change="handleCheckAllChange"
                        ></el-checkbox>
                        <el-checkbox label="专利" value="1"></el-checkbox>
                        <el-checkbox label="商标" value="2"></el-checkbox>
                        <el-checkbox label="版权" value="3"></el-checkbox>
                        <el-checkbox label="科技服务" value="4"></el-checkbox>
                        <el-checkbox label="法律案件" value="5"></el-checkbox>
                        <el-checkbox label="调查案" value="6"></el-checkbox>
                    </el-checkbox-group>
                </el-form-item>

                <el-form-item label="联系人编号" prop="id">
                    <el-input
                        class="w-[215px]"
                        v-model="formData.id"
                        placeholder="请输入联系人编号"
                    />
                </el-form-item>
                <el-form-item label="联系人名称" prop="name">
                    <el-input
                        class="w-[220px]"
                        v-model="formData.name"
                        placeholder="请输入联系人名称"
                    />
                </el-form-item>
                <el-form-item label="工作邮箱" prop="workEmail">
                    <el-input
                        class="w-[215px]"
                        v-model="formData.workEmail"
                        placeholder="请输入工作邮箱"
                    />
                </el-form-item>
                <el-form-item label="职位" prop="position">
                    <el-input
                        class="w-[220px]"
                        v-model="formData.position"
                        placeholder="请输入职位"
                    />
                </el-form-item>
                <el-form-item label="私人邮箱" prop="email">
                    <el-input
                        class="w-[215px]"
                        v-model="formData.email"
                        placeholder="请输入私人邮箱"
                    />
                </el-form-item>
                <el-form-item label="是否在职" prop="isOnJob">
                    <el-select
                        class="flex-1"
                        v-model="formData.isOnJob"
                        placeholder="请选择是否在职"
                    >
                        <el-option label="是" value="1" />
                        <el-option label="否" value="2" />
                    </el-select>
                </el-form-item>
                <el-form-item label="称呼" prop="call">
                    <el-input class="w-[215px]" v-model="formData.call" placeholder="请输入称呼" />
                </el-form-item>
                <el-form-item label="性别" prop="sex">
                    <el-select class="flex-1" v-model="formData.sex" placeholder="请选择性别">
                        <el-option label="男" value="1" />
                        <el-option label="女" value="2" />
                    </el-select>
                </el-form-item>

                <el-form-item label=" 座机" prop="number">
                    <el-input
                        class="w-[215px]"
                        v-model="formData.number"
                        placeholder="请输入 座机"
                    />
                </el-form-item>
                <el-form-item label="联系人类别" prop="type">
                    <el-select
                        class="flex-1"
                        v-model="formData.type"
                        placeholder="请选择联系人类别"
                    >
                        <el-option label="流程人员" value="1" />
                        <el-option label="技术人员" value="2" />
                    </el-select>
                </el-form-item>
                <el-form-item label="电话" prop="phone">
                    <el-input class="w-[215px]" v-model="formData.phone" placeholder="请输入电话" />
                </el-form-item>
                <el-form-item label="传真" prop="fax">
                    <el-input class="w-[220px]" v-model="formData.fax" placeholder="请输入传真" />
                </el-form-item>
                <el-form-item label="qq" prop="qq">
                    <el-input class="w-[215px]" v-model="formData.qq" placeholder="请输入qq" />
                </el-form-item>
                <el-form-item label="微信号" prop="wechat">
                    <el-input
                        class="w-[220px]"
                        v-model="formData.wechat"
                        placeholder="请输入微信号"
                    />
                </el-form-item>
                <el-form-item label="排序" prop="sort">
                    <el-input class="w-[215px]" v-model="formData.sort" placeholder="请输入排序" />
                </el-form-item>
                <el-form-item label="工作地址" prop="workAddress">
                    <el-input
                        class="w-[220px]"
                        v-model="formData.workAddress"
                        placeholder="请输入工作地址"
                    />
                </el-form-item>
                <el-form-item label="历史地址" prop="oldAddress">
                    <el-input
                        class="w-[215px]"
                        v-model="formData.oldAddress"
                        placeholder="请输入历史地址"
                    />
                </el-form-item>
                <el-form-item label="家庭地址" prop="homeAddress">
                    <el-input
                        class="w-[220px]"
                        v-model="formData.homeAddress"
                        placeholder="请输入家庭地址"
                    />
                </el-form-item>
                <el-form-item label="邮编" prop="postcode">
                    <el-input
                        class="w-[215px]"
                        v-model="formData.postcode"
                        placeholder="请输入邮编"
                    />
                </el-form-item>
                <el-form-item label="兴趣爱好" prop="hobby">
                    <el-input
                        class="w-[220px]"
                        v-model="formData.hobby"
                        placeholder="请输入兴趣爱好"
                    />
                </el-form-item>
                <el-form-item label="备注" prop="remark">
                    <el-input
                        class="w-[215px]"
                        v-model="formData.remark"
                        placeholder="请输入备注"
                    />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import { linkmanEdit, linkmanAdd, linkmanDetail } from '@/api/linkman'
import Popup from '@/components/popup/index.vue'
import feedback from '@/utils/feedback'
import type { PropType } from 'vue'
defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑线索联系人' : '新增联系人'
})

const formData = reactive({
    typevalues: [],
    id: '',
    name: '',
    workEmail: '',
    position: '',
    email: '',
    isOnJob: '',
    call: '',
    sex: '',
    type: '',
    number: '',
    phone: '',
    fax: '',
    qq: '',
    wechat: '',
    sort: '',
    workAddress: '',
    oldAddress: '',
    homeAddress: '',
    postcode: '',
    hobby: '',
    remark: ''
})
const handleCheckAllChange = async (val: any) => {
    if (val) {
        formData.typevalues = ['全选', '专利', '商标', '版权', '科技服务', '法律案件', '调查案']
        val = false
    } else {
        formData.typevalues = []
    }
}
const formRules = {
    name: [
        {
            required: true,
            message: '请输入联系人名称',
            trigger: ['blur']
        }
    ],
    phone: [
        {
            required: true,
            message: '请输入联系人电话',
            trigger: ['blur']
        }
    ],
    workAddress: [
        {
            required: true,
            message: '请输入工作地址',
            trigger: ['blur']
        }
    ],
    type: [
        {
            required: true,
            message: '请选择联系人类别',
            trigger: ['blur']
        }
    ]
}

const handleSubmit = async () => {
    await formRef.value?.validate()
    const data: any = { ...formData }
    mode.value == 'edit' ? await linkmanEdit(data) : await linkmanAdd(data)
    popupRef.value?.close()
    feedback.msgSuccess('操作成功')
    emit('success')
}

const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

const setFormData = async (data: Record<string, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
}

const getDetail = async (row: Record<string, any>) => {
    const data = await linkmanDetail({
        id: row.id
    })
    setFormData(data)
}

const handleClose = () => {
    emit('close')
}

defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
